package com.doudou.oauth.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler;

/**
 * @Author: 傻男人
 * @Date: 2020/6/10 9:40
 * @Version: 1.0
 * @Description: 用户自动同意授权
 */
public class AutoUserApprovalHandler extends ApprovalStoreUserApprovalHandler {


    private ApprovalStore approvalStore;

    @Override
    public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
//        String clientId = authorizationRequest.getClientId();
//        Collection<Approval> userApprovals = approvalStore.getApprovals(userAuthentication.getName(), clientId);
//        if(userApprovals.isEmpty()){
//            authorizationRequest.setApproved(true);
//        }
        authorizationRequest.setApproved(true);
        return authorizationRequest;
    }

    @Override
    public AuthorizationRequest updateAfterApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
        //return super.updateAfterApproval(authorizationRequest, userAuthentication);
        authorizationRequest.setApproved(true);
        return authorizationRequest;
    }

    public ApprovalStore getApprovalStore() {
        return approvalStore;
    }

    @Override
    public void setApprovalStore(ApprovalStore approvalStore) {
        super.setApprovalStore(approvalStore);
        this.approvalStore = approvalStore;
    }
}

